import request from '@/utils/request'

// 接口类型定义
export interface LoginStatsVO {
  totalUsers: number
  monthlyAppointments: number
  userTrend: number
  appointmentTrend: number
  todayAppointments: number
  activeUsers: number
  systemLoad: string
  totalLogins: number
  uniqueUsers: number
  successCount: number
  failCount: number
  successRate: number
  avgDuration: number
  peakHour: number
  statTime: string
}

export interface TimeDistributionVO {
  hour: number
  count: number
}

export interface UserActiveVO {
  username: string
  loginCount: number
  lastLoginTime: string
}

export interface CounselorWorkloadData {
  counselorName: string
  appointmentCount: number
}

export interface Appointment {
  id: number
  studentName: string
  counselorName: string
  appointmentTime: string
  status: 'PENDING' | 'CONFIRMED' | 'CANCELLED' | 'COMPLETED'
}

export interface LoginLog {
  id: number
  userId: number
  username: string
  ip: string
  location: string
  browser: string
  os: string
  status: number
  msg: string
  createTime: string
  logoutTime?: string
  loginDuration?: number
  userAgent?: string
  deviceType: string
}

export interface DistributionItem {
  name: string
  value: {
    count: number
    avg_duration: number
    location?: string
    device_type?: string
  }
}

export interface RegisterLog {
  id: number
  userId: number
  username: string
  role: string
  status: string
  ip: string
  location: string
  browser: string
  os: string
  deviceType: string
  createTime: string
}

// 咨询师仪表盘相关接口
export function getCounselorStatistics() {
  return request({
    url: '/dashboard/counselor/statistics',
    method: 'get'
  })
}

export function getTodaySchedule() {
  return request({
    url: '/dashboard/counselor/schedule',
    method: 'get'
  })
}

export function getTodoList() {
  return request({
    url: '/dashboard/counselor/todos',
    method: 'get'
  })
}

// 登录分析相关接口
export function getLoginStats(params?: { startDate?: string; endDate?: string }) {
  return request<LoginStatsVO>({
    url: '/analysis/login/stats',
    method: 'get',
    params
  })
}

export function getActiveUsers(timeRange: string = 'day') {
  return request<UserActiveVO[]>({
    url: '/analysis/login/active-users',
    method: 'get',
    params: { timeRange }
  })
}

export function getLoginTimeDistribution(date?: string) {
  return request<TimeDistributionVO[]>({
    url: '/analysis/login/time-distribution',
    method: 'get',
    params: { date }
  })
}

export function getLoginGeoDistribution() {
  return request<DistributionItem[]>({
    url: '/analysis/login/geo-distribution',
    method: 'get'
  })
}

export function getDeviceDistribution() {
  return request<DistributionItem[]>({
    url: '/analysis/login/device-distribution',
    method: 'get'
  })
}

// 预约相关接口
export function getAppointmentTrend(days: number) {
  return request<{ dates: string[]; counts: number[] }>({
    url: '/counseling/appointment/trend',
    method: 'get',
    params: { days }
  })
}

export function getCounselorWorkload() {
  return request<CounselorWorkloadData[]>({
    url: '/counseling/counselor/workload',
    method: 'get'
  })
}

export function getLatestAppointments() {
  return request<Appointment[]>({
    url: '/counseling/appointment/latest',
    method: 'get'
  })
}

export function getLoginLogs() {
  return request<LoginLog[]>({
    url: '/system/login/log',
    method: 'get'
  })
}

// 获取注册日志
export function getRegisterLogs() {
  return request<RegisterLog[]>({
    url: '/system/register/log',
    method: 'get'
  })
} 